package com.sgb.kjwl.view.ui.activity.self.mine;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.jyb.qiyefuwu.view.fragment.usercenter.ServiceAiAttentionFragment;
import com.sgb.auction.fragment.FocusAuctionListFragment;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityIntelligentAttentionBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.swgk.core.view.entity.ChannelEntity;
import com.sgb.kjwl.utils.NumberConstant;
import com.sgb.kjwl.view.ui.adapter.home.HomeFragmentPagerAdapter;
import com.sgb.kjwl.view.ui.fragment.packers.PackerAttentionDetailFragment;
import com.sgb.kjwl.view.ui.fragment.pick.PickAttentionDetailFragment;
import com.sgb.kjwl.view.ui.fragment.self.mine.CommonFocusFragment;
import com.sgb.kjwl.view.ui.fragment.self.mine.IntelligentFocusListFragment;
import com.sgb.kjwl.view.ui.fragment.self.mine.UserOrganizationAttentionFragment;
import com.sgb.kjwl.viewmodel.self.mine.IntelligentFocusModel;
import com.sgb.auction.fragment.AuctionHallFragment;
import com.swgk.core.base.BaseFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;


/**
 * description:智能关注 主页
 *
 * @author chenT
 * @date 2021/03/01
 */
public class IntelligentAttentionActivity extends AppActivity {

    private ActivityIntelligentAttentionBinding binding;
    public MutableLiveData<Integer> liveData = new MutableLiveData<>();

    @Inject
    IntelligentFocusModel viewModel;

    private List<ChannelEntity> channelEntityList;

    private List<BaseFragment> chooseFragments;

    /**
     * 选中时标题栏目字体大小
     */
    private float selectedSize;
    private TextView selectTextView;
    private int type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(IntelligentAttentionActivity.this, R.layout.activity_intelligent_attention);
        binding.setViewModel(viewModel);
        viewModel.getDataList();
        selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 18, getResources().getDisplayMetrics());
        chooseFragments = new ArrayList<>();
        channelEntityList = new ArrayList<>();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, IntelligentAttentionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, IntelligentAttentionActivity.class);
        intent.putExtra("type", type);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void init() {
        type = getIntent().getIntExtra("type", 0);
        NumberConstant.sIsChangeForTeam = 1;
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("智能关注");
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        binding.tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (selectTextView == null) {
                    selectTextView = new TextView(getActivity());
                }
                selectTextView.setTextSize(selectedSize);
                selectTextView.setPadding(0, 0, 0, 10);
                selectTextView.getPaint().setFakeBoldText(true);
                selectTextView.setTextColor(getResources().getColor(R.color.white));
                selectTextView.setText(tab.getText());
                tab.setCustomView(selectTextView);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public void getDataList(Map<String, String> mapList) {
        if (mapList == null) {
            return;
        }

        if (mapList.size() <= 0) {
            mapList.put("0", "企业");
            mapList.put("1", "商品");
            mapList.put("2", "招采");
            mapList.put("3", "拍卖");
            mapList.put("4", "集采");
            mapList.put("5", "工队");
            mapList.put("6", "租赁");
            mapList.put("7", "物流");
            mapList.put("8", "企服");
            mapList.put("9", "个人");
            mapList.put("10", "团队");
            mapList.put("11", "组织");
        }

        channelEntityList.clear();
        chooseFragments.clear();
        Iterator iter = mapList.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            ChannelEntity channelEntity = new ChannelEntity();
            channelEntity.setName(entry.getValue().toString());
            channelEntity.setId(Integer.parseInt(entry.getKey().toString()));
            channelEntityList.add(channelEntity);
            if ("企业".equals(entry.getValue().toString())) {
                chooseFragments.add(IntelligentFocusListFragment.newInstance(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, ""), Integer.parseInt(entry.getKey().toString())));
            } else if ("招采".equals(entry.getValue().toString())) {
                chooseFragments.add(PickAttentionDetailFragment.newInstance());
            } else if ("拍卖".equals(entry.getValue().toString())) {
                chooseFragments.add(FocusAuctionListFragment.newInstance());
            } else if ("工队".equals(entry.getValue().toString())) {
                chooseFragments.add(PackerAttentionDetailFragment.newInstance());
            } else if ("团队".equals(entry.getValue().toString())) {
                chooseFragments.add(UserOrganizationAttentionFragment.Companion.newInstance(UserOrganizationAttentionFragment.TYPE_GROUP));
            } else if ("个人".equals(entry.getValue().toString())) {
                chooseFragments.add(UserOrganizationAttentionFragment.Companion.newInstance(UserOrganizationAttentionFragment.TYPE_PERSON));
            } else if ("企服".equals(entry.getValue().toString())) {
                chooseFragments.add(ServiceAiAttentionFragment.newInstance());
            } else if ("组织".equals(entry.getValue().toString())) {
                chooseFragments.add(UserOrganizationAttentionFragment.Companion.newInstance(UserOrganizationAttentionFragment.TYPE_ORGANIZATION));
            } else {
                chooseFragments.add(CommonFocusFragment.newInstance(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, ""), Integer.parseInt(entry.getKey().toString())));
            }
        }
        HomeFragmentPagerAdapter goodsFragmentPagerAdapter = new HomeFragmentPagerAdapter(getSupportFragmentManager(), chooseFragments, channelEntityList);
        binding.viewpager.setAdapter(goodsFragmentPagerAdapter);
        binding.tablayout.setupWithViewPager(binding.viewpager);
        binding.viewpager.setCurrentItem(type);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (getSupportFragmentManager().getFragments() != null && getSupportFragmentManager().getFragments().size() > 0) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            for (Fragment mFragment : fragments) {
                if (mFragment != null && mFragment instanceof PickAttentionDetailFragment) {
                    PickAttentionDetailFragment pickattentiondetailfragment = ((PickAttentionDetailFragment) mFragment);
                    if (pickattentiondetailfragment.isInIt) {
                        pickattentiondetailfragment.onActivityResult(requestCode, resultCode, data);
                    }
                }
                if (mFragment != null && mFragment instanceof FocusAuctionListFragment) {
                    FocusAuctionListFragment focusauctionlistfragment = ((FocusAuctionListFragment) mFragment);
                    if (focusauctionlistfragment.isInIt) {
                        focusauctionlistfragment.onActivityResult(requestCode, resultCode, data);
                    }
                }
            }
        }

        if (resultCode == NumberConstant.NUMBER_TWO || resultCode == NumberConstant.NUMBER_ONE) {
            liveData.postValue(1);
        }
    }
}